package com.k2091.washcar.controller;


import com.k2091.washcar.entity.FormInline;
import com.k2091.washcar.entity.Users;
import com.k2091.washcar.entity.UsersCar;
import com.k2091.washcar.service.IUsersService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/api/users")
public class UsersController {

    @Autowired
    private IUsersService usersService;

    /**
     * 用户添加车辆信息
     *
     * @param car
     * @return
     */
    @RequestMapping("/addCar")
    @ResponseBody
    public int insertCar(@RequestBody UsersCar car) {
        int r = usersService.addCar(car);
        return r;
    }

    //用户查询车辆 根据用户id 查该用户的车辆
    @RequestMapping("/selectById")
    public List<UsersCar> selectByUserId(Long id) {
        List<UsersCar> usersCars = usersService.selectByUserId(id);
//        HashMap map = new HashMap();
//        map.put("usercars", usersCars);
//        return map;
        return usersCars;
    }

    /**
     * 用户完善个人信息(前端获取id)
     * 身份证号 手机号不能为空
     *
     * @param users
     * @return
     */
    @RequestMapping("/di")
    @ResponseBody
    public int complete(@RequestBody Users users) {
        int r = usersService.insertMessage(users);
        return r;
    }

    @RequestMapping("/getUserList")
    public List<Users> getUserList(FormInline formInline) {
        System.out.println(formInline.toString());
        List<Users> users = usersService.selectUser(formInline);

//        JSONArray jsonArray = JSONArray.fromObject(users);
        return users;
    }


    @RequestMapping("/addUser")
    public void addUser(Users users){
        Long id = users.getId();
        if (usersService.selectUsersById(id)==null){
            users.setCreatetime(LocalDateTime.now());
            usersService.addUser(users);
        }else {
            usersService.upUser(users);
        }
    }

    @RequestMapping(value = "/upStatus",method = RequestMethod.GET)
    public boolean upStatus(@Param("id")Long id,@Param("status")int status,@Param("token")String token){
        int num = usersService.upStatus(id,status);
        if (num!=0){
            return true;
        }else{
            return false;
        }
    }

    @RequestMapping("/deleteUser")
    public void deleteUser(Long id,String token){
        usersService.userDelete(id);
    }


}
